package com.ljw.gulimall.ware.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ljw.common.exception.BizCodeEnum;
import com.ljw.common.to.ware.WareSkuTo;
import com.ljw.gulimall.ware.exception.NoStockException;
import com.ljw.gulimall.ware.vo.LockStockResult;
import com.ljw.gulimall.ware.vo.SkuHasStockVo;
import com.ljw.gulimall.ware.vo.WareSkuLockVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ljw.gulimall.ware.entity.WareSkuEntity;
import com.ljw.gulimall.ware.service.WareSkuService;
import com.ljw.common.utils.PageUtils;
import com.ljw.common.utils.R;



/**
 * 商品库存
 *
 * @author molimicha
 * @email 1328388970@qq.com
 * @date 2022-08-07 03:50:47
 */
@RestController
@RequestMapping("ware/waresku")
public class WareSkuController {
    @Autowired
    private WareSkuService wareSkuService;

    @PostMapping("/lock/order")
    public R orderLockStock(@RequestBody WareSkuLockVo vo){
        try {
            Boolean stock = wareSkuService.orderLockStock(vo);
            return R.ok();
        }catch (NoStockException exception){
            exception.printStackTrace();
            return R.error(BizCodeEnum.NO_STOCK_EXCEPTION.getCode(), BizCodeEnum.NO_STOCK_EXCEPTION.getMessage());
        }
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = wareSkuService.queryPage(params);

        return R.ok().put("page", page);
    }

    //

    /**
     * 查询skuIds是否有库存
     */
    @PostMapping("/hasstock")
    public R getSkuHasStock(@RequestBody List<Long> skuIds){
        List<SkuHasStockVo> vos = wareSkuService.getSkuHasStock(skuIds);
        return R.ok().put("data", vos);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		WareSkuEntity wareSku = wareSkuService.getById(id);

        return R.ok().put("wareSku", wareSku);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody WareSkuEntity wareSku){
		wareSkuService.save(wareSku);

        return R.ok();
    }

    @PostMapping("/saveBatch")
    public R saveBatch(@RequestBody List<WareSkuTo> wareSkuTos){
        List<WareSkuEntity> collect = wareSkuTos.stream().map(wareSkuTo -> {
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            BeanUtils.copyProperties(wareSkuTo, wareSkuEntity);
            return wareSkuEntity;
        }).collect(Collectors.toList());
        wareSkuService.saveBatch(collect);
        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody WareSkuEntity wareSku){
		wareSkuService.updateById(wareSku);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		wareSkuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
